// eslint-disable-next-line no-unused-vars
const TronWeb = require("tronweb")
//私钥
const privateKey='CF1CD5586390FEA4B1537BD24061B0A89412C4DCD3FE4DC83E3EE11FA78AE4FD'
const type = 'nile';
let tronWeb = null;

if(type === "nile"){
    tronWeb = new TronWeb(
        'https://api.nileex.io/',
        'https://api.nileex.io/',
        'https://event.nileex.io/',
        'CF1CD5586390FEA4B1537BD24061B0A89412C4DCD3FE4DC83E3EE11FA78AE4FD',
        // privateKey 添加了这个私钥 那么转账啥的就可以快捷转账
    );
}else if(type === 'shasta'){
    tronWeb = new TronWeb(
        'https://api.shasta.trongrid.io',
        'https://api.shasta.trongrid.io',
        'https://api.shasta.trongrid.io',
        'CF1CD5586390FEA4B1537BD24061B0A89412C4DCD3FE4DC83E3EE11FA78AE4FD',
        // privateKey
    );
}


class TronUtils {
    constructor() {
        console.log("可以修改tronWeb的构造方法，把私钥放开："+privateKey)
    }

    /**
     * 获取环境类型
     * @return {string}
     */
    getNetType = function(){
        return type
    };
    /**
     * 获取指定地址的trx数量
     * @param address
     * @returns {Promise<number>}
     */
    balanceOf = async function (address) {
        let balance = await tronWeb.trx.getBalance(address);
        return Number(tronWeb.fromSun(balance).toString());
    };

    /**
     * 获取当前地址
     * @returns {Promise<unknown>}
     */
    getAddress = function () {
        return tronWeb.address.getAddress();
    };

    /**
     * 转换为hex地址
     * @param addr
     * @returns {*}
     */
    addressToHex = function (addr) {
        return tronWeb.address.toHex(addr);
    };

    /**
     * hex地址转成T开头的地址
     * @param hexAddr
     * @returns {*}
     */
    addressFromHex = function (hexAddr) {
        return tronWeb.address.fromHex(hexAddr);
    };

    /**
     * 创建tron地址
     */
    createAccount = function () {
        return tronWeb.createAccount();
    };

    getTronWeb = function(){
        return tronWeb;
    }
}

const tronUtils = new TronUtils();

export default tronUtils